package imgshare.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import imgshare.common.api.CommonResult;
import imgshare.model.Collect;
import imgshare.model.vo.CollectState;
import imgshare.service.CollectService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import javax.websocket.server.PathParam;
import java.util.List;

@RestController
public class CollectController {

    @Autowired
    private CollectService collectService;
    private static final Log log = LogFactory.get();

    /** 查询用户收藏图片集 id列表 */
    @GetMapping("/collect")
    public CommonResult<?> getCollectState(@RequestParam Integer userId) {
        List<Integer> collectedImageIdList = collectService.getCollectState(userId);
        return CommonResult.success(collectedImageIdList);
    }

    /** 更新收藏状态，已收藏->取消收藏；未收藏->收藏 */
    @PostMapping("/collect_switch")
    @RequiresAuthentication
    public CommonResult<?> switchCollectState(@NotNull @RequestParam("imageId") Integer imageId,
                                              @NotNull @RequestParam("userId") Integer userId) {
        Collect foundedCollect = collectService.findOne(imageId, userId);
        if (foundedCollect != null) {
            collectService.cancelCollect(foundedCollect);
            return CommonResult.success("取消收藏成功");
        }
        collectService.addCollect(imageId,userId);
        return CommonResult.success("收藏成功");
    }

}
